package com.example.testapp.activity.activity.menu;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.example.testapp.R;
import com.example.testapp.activity.dabastate.CalendarDao;
import com.example.testapp.activity.model.CalendarVO;
import com.example.testapp.activity.util.CalendarContract;
import com.example.testapp.activity.view.BorderTextView;

import java.util.ArrayList;

/**
 * Created by l on 2016/6/11.
 */
public class CalendarAllActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {

    private ScrollView  sv = null;
    private LinearLayout layout = null;
    private BorderTextView textTop = null;
    private CalendarDao  Dao = null;
    private CalendarVO  calendarVO = null;
    private ArrayList<CalendarVO> calendarVOArrayList = new ArrayList<>();
    private String  calendarInfo = " ";
    private LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    private int calendarID = -1;
    private PopupMenu  popupMenu = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Dao = new CalendarDao(this);
        sv =new ScrollView(this);

        params.setMargins(0, 5, 0, 0);
        layout = new LinearLayout(this);// 实例化布局对象
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackgroundResource(R.mipmap.schedule_bk);
        layout.setLayoutParams(params);

        textTop = new BorderTextView(this, null);
        textTop.setTextColor(Color.BLACK);
        textTop.setBackgroundResource(R.mipmap.top_day);
        textTop.setText("所有日程");
        textTop.setHeight(80);
        textTop.setGravity(Gravity.CENTER);

        layout.addView(textTop);
        sv.addView(layout);

        setContentView(sv);

        getCalendarAll();
    }

    /**
     * 得到所有的日程信息
     */
    private void getCalendarAll() {
        calendarVOArrayList = Dao.getAllCalendar();
        if(calendarVOArrayList != null){
            for (CalendarVO vo : calendarVOArrayList){
                String  content = vo.getScheduleContent();
                int startLine = content.indexOf("\n");
                if(startLine > 0){
                     content = content.substring(0, startLine) + "...";
                }else if(content.length() > 30){
                      content = content.substring(0, 30) + "...";
                }
                calendarInfo = CalendarContract.sch_type[vo.getScheduleTypeID()]+"\n"+vo.getScheduleDate()+"\n"+content;
                calendarID = vo.getScheduleID();
                createInfoText(calendarInfo, -1);
            }
        }else {
            calendarInfo = "没有日程";
            createInfoText(calendarInfo, -1);
        }
    }

    /**
     * 创建放日程信息的textview
     */
    private void createInfoText(String calendarInfo, int calendarID) {
        BorderTextView info = new BorderTextView(this, null);
        info.setText(calendarInfo);
        info.setTextColor(Color.BLACK);
        info.setBackgroundColor(Color.WHITE);
        info.setLayoutParams(params);
        info.setGravity(Gravity.CENTER_VERTICAL);
        info.setPadding(10, 5, 10, 5);
        info.setHeight(80);
        info.setTag(calendarID);
        layout.addView(info);

        info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String calID = String.valueOf(v.getTag());
                String scheduleIDs[] = new String[]{calID};
                Intent intent = new Intent();
                intent.setClass(CalendarAllActivity.this, CalendarInfoViewActivity.class);
                intent.putExtra("calendarID", scheduleIDs);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_calendar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.calendar:
                popupMenu = new PopupMenu(CalendarAllActivity.this, findViewById(R.id.calendar));
                popupMenu.getMenuInflater().inflate(R.menu.menu_tag, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(this);
                popupMenu.show();
                break;
        }
        return true;
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()){
            case R.id.back_calendar:
                startActivity(new Intent(CalendarAllActivity.this, DateActivity.class));
                break;
            case R.id.add_calendar:
                startActivity(new Intent(CalendarAllActivity.this, CalendarViewActivity.class));
                break;
        }
        return true;
    }
}
